<?php

namespace app\roomcall\controller;


use app\common\controller\Alipay;
use app\common\controller\Smscm;
use app\common\controller\Wxpay;
use app\common\model\Activ;
use app\common\model\CommissionLog;
use app\common\model\Message as MessageModel;
use app\common\model\Opinion;
use app\common\model\Order;
use app\common\model\Orderproject;
use app\common\model\Room;
use app\common\model\Roomappointment;
use app\common\model\Userphoto;
use app\common\model\Users as UserM;
use app\common\model\ViperCard;
use app\common\model\ViperUser;
use app\common\model\VipOrder as VipOrderM;
use think\Db;
use function AlibabaCloud\Client\envNotEmpty;

/**
 * 前台首页控制器
 * @package app\api\controller
 */
class User extends Home
{
    public $noNeedLogin = [''];

    /**
     *  个人中心首页
     **/
    public function index()
    {

        if ($this->request->isPost()) {
            $userInfo = $this->userInfo;
            $data['real_name'] = $userInfo['real_name'];
            $data['avatar'] = get_img_path($userInfo['avatar']);
            $data['phone'] = $userInfo['phone'];
            $data['work_number'] = $userInfo['work_number'];
            $data['time_type'] = $userInfo['time_type'];
            $this->result('获取成功', $data, 200);
        } else {
            $this->result('非法请求', '', 204);
        }
    }

    /**
     *反馈
     */
    public function report()
    {
        $user_id = $this->userInfo['id'];
        $content = $this->request->post('content');
        $arr['user_id'] = $user_id;
        $arr['created_time'] = time();
        $arr['content'] = $content;
        $arr['status'] = 1;
        if (Opinion::create($arr)) {
            $this->result('提交成功', '', 200);
        } else {
            $this->result('提交失败', '', 204);
        }
    }

    /**
     * 呼叫服务
     */
    public function callService() {

        $post = $this->request->post();
        if (empty($post['room_id'])) {
            $this->result('房间ID不能为空', '', 204);
        }

        $room_info = Db::name('qt_room')->where('id',$post['room_id'])->find();
        $group_user = Db::name('qt_users')->where(['group'=>2, 'status'=>0])->select();

        if (empty($room_info)) {
            $this->result('房间没有找到', '', 204);
        }
        foreach ($group_user as $key => $item) {

            $data = [
                'user_id'=>$this->userInfo['id'],//发起人
                'to_user_id'=>$item['id'],//接收人
                'title'=>'房间'.$room_info['name'].'呼叫服务',//消息标题
                'room_id'=>$room_info['id'],//房间ID
                'order_id'=>0,//订单ID
                'content'=>'房间'.$room_info['name'].'呼叫服务',
                'status'=>1,//状态  1 正常 2 已通过 3 未通过
                'read'=>0,//阅读 0 未读 1 已读
                'type'=>2,//类型 1 系统消息 2 呼叫服务消息 3 商品下单 4 技师下钟消息 5 加钟消息 6 技师提前下班消息 7 技师上班迟到消息 8
                'created_time'=>time(),
            ];

            Db::name('qt_message')->insert($data);
        }
        $this->result('呼叫成功！', '', 200);
    }

    /**
     * 搜索用户
     */
    public function search(){
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            if(!isset($getData['phone']) || empty($getData['phone'])){
                $this->result('缺少参数', '', 204);
            }
            $result =Db::name('qt_user_viper')->alias('uv')
                ->field('uv.id,uv.phone,uv.nickname,r.name')
                ->leftJoin('qt_rank r','uv.rank = r.id')
                ->where(['uv.phone'=>$getData['phone']])
                ->find();
            $this->result('查询成功', $result, 200);
        }
        $this->result('无效的请求方式', '', 204);
    }

}
